﻿using QDAS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using WindGoes.IO;

namespace QTrans
{
	public partial class CataLogForm : Form
	{
		ComboBox[] cbs;
		IniAccess ia = new IniAccess();
		int[] indexes;

		/// <summary>
		/// cbK4113, cbK4253, cbK4093, cbK4063, cbK4073, cbK4273, cbK4283, cbK4292
		/// </summary>
		public string[] Values { get; set; }

		public CataLogForm(string file, int[] indexes)
		{
			InitializeComponent();
			this.indexes = indexes;
			Values = new string[8];

			QCatalog catalog = QCatalog.load(Path.GetFullPath(File.Exists("CATALOG_渝江压铸.dfd") ? "CATALOG_渝江压铸.dfd" : file));
			cbs = new ComboBox[] { cbK4113, cbK4253, cbK4093, cbK4063, cbK4073, cbK4273, cbK4283, cbK4292 };
			// indexes = indexes ?? new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };

			for (int i = 0; i < cbs.Length; i++)
			{
				List<QLineInfo> infos = catalog.GetLineInfo(cbs[i].Name.Substring(2));
				foreach (var item in infos)
					cbs[i].Items.Add(item.pid + "." + item.value);
				cbs[i].Tag = infos;
				cbs[i].Items.Add(" ");
			}


			for (int i = 0; i < cbs.Length; i++)
			{
				cbs[i].SelectedIndex = indexes[i];
			}
		}

		private void CataLogForm_Load(object sender, EventArgs e)
		{
			Canceled = true;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			Canceled = true;
			Close();
		}

		public bool Canceled { get; set; }

		private void button1_Click(object sender, EventArgs e)
		{
			// 如果选择第一个，会不转换。 
			Values[0] = cbs[0].Text == " " ? null : (cbs[0].Tag as List<QLineInfo>)[cbs[0].SelectedIndex].value;
 

			for (int i = 0; i < cbs.Length; i++)
			{
				indexes[i] = cbs[i].SelectedIndex;
				Values[i] = cbs[i].Text == " " ? null : (cbs[0].Tag as List<QLineInfo>)[cbs[i].SelectedIndex].value;
			}
			Canceled = false;
			Close();
		}
	}
}
